<template xmlns="">
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="65px" label-position="left" class="login-form" >
      <h3 class="title">后台管理系统</h3>
      <el-tabs v-model="loginForm.loginType" @tab-click="handleTabs">
        <el-tab-pane label="个  人  账  号  登  录" name="personal"/>
        <el-tab-pane label="单  位  账  号  登  录" name="company"/>
      </el-tabs>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="loginForm.userName" placeholder="请输入用户名">
          <i slot="prefix" class="el-input__icon el-icon-user"/>
        </el-input>
      </el-form-item>
      <el-form-item label="密  码" prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码">
          <i slot="prefix"  class="el-input__icon el-icon-user"/>
        </el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="loginForm.code" type="text" placeholder="请输入验证码" uto-complete="off" style="width: 52%" @keyup.enter.native="handleLogin">
          <i slot="prefix"  class="el-input__icon el-icon-picture-outline-round"/>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" alt="验证码"/>
        </div>
      </el-form-item>
      <el-form-item label="记住我" prop="rememberMe">
        <el-checkbox v-model="loginForm.rememberMe"/>
      </el-form-item>
      <el-button :loading="loading" type="primary" size="medium" @click.native.prevent="handleLogin" style="width: 100%;margin-bottom: 14px">
        <span v-if="!loading">登 录</span>
        <span v-else>登 录 中...</span>
      </el-button>
      <div style="float: right;" v-if="register">
        <router-link class="link-type" :to="'/register'">立即注册</router-link>
      </div>
    </el-form>
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 wj.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import { encrypt } from '@/utils/jsencrypt'


export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        userName: "admin",
        password: "admin123",
        rememberMe: true,
        code: "",
        uuid: "",
        loginType:'personal'
      },
      codeUrl: "",
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur",},
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 20, message: "长度在 5 到 20 个字符", trigger: "blur" },
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "change" },],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: "/main",
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("userName", this.loginForm.userName, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("userName");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("user/Login", this.loginForm).then(() => {
            this.$message({
              message: this.loginForm.userName+ ",登录成功",
              type: "success",
            });
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
    handleTabs(tab, event) {
      //console.log(tab, event);
      this.loginForm.loginType = tab.name;
    },
    getCode() {
      getCodeImg().then(res => {
        // console.log(res);
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/scss" lang="scss">
.title {
  text-align: left;
  margin:  12px auto ;
  color: #707070;
}
.content {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  box-shadow: 0 8px 16px silver;
  /* background-color: rgba(29, 67, 89,0.5); */
}
.login{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url(../../assets/images/login-background.jpg);
  background-size: cover;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 380px;
  padding: 15px;
  box-shadow: 0 8px 16px silver;
  .el-form-item{
    margin-bottom: 8px;
  }
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .el-checkbox {
    height: 38px;
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-code {
  width: 48%;
  height: 38px;
  margin-right: -10px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgb(216, 18, 18);
  font-family: Arial,serif;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
